package com.nursinghome.user.controller;


import com.nursinghome.annotation.Log;
import com.nursinghome.bean.Reservation;
import com.nursinghome.bean.RespBean;
import com.nursinghome.common.service.ReservationService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user/reservation")
public class UserReServiceController {
    @Autowired
    private ReservationService reservationService;
    @GetMapping
    public RespBean selectAll(@RequestHeader(value = "token") String token) {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        Reservation reservation = new Reservation();
        reservation.setEid(eid);
        reservation.setStatus(2);
        List<Reservation> list = reservationService.selectAllByCondition(reservation);
        return RespBean.ok("",list);
    }
    @GetMapping("/after")
    public RespBean selectByAfter(@RequestHeader(value = "token") String token) {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        Reservation reservation = new Reservation();
        reservation.setEid(eid);
        reservation.setStatus(1);
        List<Reservation> list = reservationService.selectAllByCondition(reservation);
        return RespBean.ok("", list);
    }
    //新增预约服务订单
    @Log(name = "预约服务")
    @PostMapping
    public RespBean insert(@RequestHeader(value = "token") String token,
                           @RequestBody Reservation reservation) throws NurisingHomeException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        reservation.setEid(eid);
        reservation.setStatus(1);
        if (reservationService.insert(reservation)) {
            return RespBean.ok("预约成功");
        }
        return RespBean.error("预约失败");
    }
    //修改预约服务订单
    @PutMapping
    public RespBean update(@RequestHeader(value = "token") String token,
            @RequestBody Reservation reservation) throws NurisingHomeException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        reservation.setEid(eid);
        if (reservationService.update(reservation)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //删除预约服务订单
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") String id) throws NurisingHomeException {
        if (reservationService.delete(id)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显预约服务订单数据
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id) throws NurisingHomeException {
        Reservation reservation = reservationService.selectById(id);
        return RespBean.ok("",reservation);
    }
}
